﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TerhelesEloszto.Common;
using TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class ModifyTask : System.Web.UI.Page
	{
		private static IEngine engine = Engine.Instance();
		ITask currentTask = null;
		protected void Page_Load( object sender, EventArgs e )
		{
			String visitorName = User.Identity.Name;
			if( !engine.IsAdmin( visitorName ) )
			{
				if( engine.IsTeacher( visitorName ) )
				{
					Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}

			}
			foreach( ITask actTask in engine.GetTasks() )
			{
				if( actTask.ID == Convert.ToInt32( Request.QueryString["ID"] ) )
				{
					currentTask = actTask;
					break;
				}
			}


			if( currentTask != null && !Page.IsPostBack )
			{
				try
				{
					loadFacts();
					loadSkills();
					Name.Text = currentTask.Name.Trim();
					HoursList.SelectedValue = currentTask.Hours.ToString();
					if( currentTask.Type.HasInterval )
					{
						HoursList.Enabled = false;
					}
					else
					{
						HoursList.Enabled = true;
					}

					switch( currentTask.Semester )
					{
						case SemesterType.autumn: SemesterList.SelectedIndex = 0; break;
						case SemesterType.spring: SemesterList.SelectedIndex = 1; break;
						case SemesterType.both: SemesterList.SelectedIndex = 2; break;
					}
				}
				catch( EngineException ex )
				{
					Response.Redirect( "~/Admin/Task.aspx?msg=0" );
				}

			}
			else if( currentTask == null )
			{
				Response.Redirect( "~/Admin/Task.aspx?msg=0" );
			}


		}

		protected void loadFacts()
		{
			int i = 0;
			foreach( IFact actFact in engine.GetFacts() )
			{
				ListItem item = new ListItem();
				item.Text = actFact.Name + "(" + actFact.Neptun + ")";
				FactList.Items.Add( item );
				if( currentTask.RelatedFact.Neptun.Equals( actFact.Neptun ) && !Page.IsPostBack )
				{
					FactList.SelectedIndex = i;
				}
				i++;
			}


		}


		protected void loadSkills()
		{
			int i = 0;
			foreach( ISkill actSkill in engine.GetSkills() )
			{
				ListItem item = new ListItem();
				item.Text = actSkill.Name;
				SkillList.Items.Add( item );
				if( currentTask.RequiredSkills.Contains( actSkill ) && !Page.IsPostBack )
				{
					SkillList.Items[i].Selected = true;
				}
				i++;

			}

		}

		protected void submit_Click( object sender, EventArgs e )
		{
			SemesterType semester = SemesterType.autumn;
			if( SemesterList.SelectedValue == "Őszi" )
			{
				semester = SemesterType.autumn;
			}
			else if( SemesterList.SelectedValue == "Tavaszi" )
			{
				semester = SemesterType.spring;
			}
			else if( SemesterList.SelectedValue == "Mindkettő" )
			{
				semester = SemesterType.both;
			}

			if( Vname.IsValid && Vskill.IsValid )
			{

				IFact fact = engine.GetFacts()[FactList.SelectedIndex];


				List<ISkill> skills = new List<ISkill>();
				foreach( int index in SkillList.GetSelectedIndices() )
				{
					skills.Add( engine.GetSkills()[index] );
				}
				try
				{
					if( !currentTask.Type.HasInterval )
						engine.UpdateTask( currentTask, fact, Name.Text, currentTask.Type, skills, Int32.Parse( HoursList.SelectedValue ), semester );
					else if( currentTask.Type.HasInterval )
						engine.UpdateTask( currentTask, fact, Name.Text, currentTask.Type, skills, currentTask.TimeIntervals, semester );
					Response.Redirect( "~/Admin/Task.aspx?msg=1" );
				}
				catch( EngineException ex )
				{
					Response.Redirect( "~/Admin/Task.aspx?msg=0" );
				}
			}

		}
	}

}